package cheme.ProcessEditor.DirectEdit;

import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.Interpreter.Interpreter;
import cheme.ProcessModel.Interpreter.ModelElementData;
import cheme.ProcessModel.Interpreter.Parsers.IParser;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.LabelField;

import org.eclipse.jface.dialogs.MessageDialog;

public class LabelFieldCellEditorvalidator extends WritableFieldCellEditorValidator 
{
	LabelField _modelElement;
	
	public LabelFieldCellEditorvalidator(ValidationMessageHandler validationMessageHandler, IParser parser, LabelField modelElement) {
		super(validationMessageHandler, parser);
		
		_modelElement = modelElement;
		// TODO Auto-generated constructor stub
	}

	@Override
	public String isValid(Object value)
	{
		String result = unsetMessageText();
		String baseResult = super.isValid(value);
		
		if(baseResult == null)
		{
			Interpreter interpreter = ModelEnv.getInstanceFor(_modelElement).getInterpreter();
			
			ModelElementData modelData = interpreter.getModelData((String)value);
			
			//System.out.println("Testing label: " + value + " for insertion to the table.");
			
			if(interpreter.modelExists((String)value) && //name is new
			   (null != modelData && _modelElement != modelData.getModel()))
			{
				result = setMessageText("The name you are trying to use is already used for another element.");
				
			}
		}
		else
		{
			result = baseResult;
		}
		
		return result;
	}
}
